import requests
import json

API_KEY = "cEkflbzrKORcVbiKGFuNeLpG"
SECRET_KEY = "LUX1ESREv8IZVFbDuvSq7MVqU2YLx6bX"

def main(string):
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + get_access_token()

    payload = json.dumps({
        "user_id": "uesr",
        "messages": [
            {
                "role": "user",
                "content": string
            }
        ],
        "temperature": None,
        "disable_search": False,
        "enable_citation": False
    })
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    data = json.loads(response.text)
    return data["result"]


def get_access_token():
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))